package com.zhuangjie.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuangjie.mvc.enhance.mp_query_enhance.LambdaQueryWrapperPlus;
import com.zhuangjie.mvc.enhance.mp_query_enhance.LambdaQueryWrapperPlusHelper;
import com.zhuangjie.security.provider.UserProvider;
import com.zhuangjie.shop.entity.ShopCollect;
import com.zhuangjie.shop.mapper.ShopCollectMapper;
import com.zhuangjie.shop.service.ShopCollectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 店铺收藏表 服务实现类
 * </p>
 *
 * @author zhuangjie
 * @since 2023-10-15
 */
@Service
public class ShopCollectServiceImpl extends ServiceImpl<ShopCollectMapper, ShopCollect> implements ShopCollectService {


    @Override
    @Transactional
    public void followUnfollowShop(Long shopId, Boolean isFollow) {
        // 取消关注-删除关注记录
        this.remove(Wrappers.lambdaQuery(ShopCollect.class)
                .eq(ShopCollect::getShopId, shopId)
                .eq(ShopCollect::getUserId, UserProvider.currentUserId()));
        // 关注-添加关注记录
        if (isFollow) {
            ShopCollect shopCollect = new ShopCollect();
            shopCollect.setShopId(shopId);
            shopCollect.setUserId(UserProvider.currentUserId());
            boolean isSaveSuccess = this.save(shopCollect);
            if (!isSaveSuccess) throw new RuntimeException("操作失败！");
        }

    }

    @Override
    public boolean isFollow(Long shopId) {
        return LambdaQueryWrapperPlusHelper.exists(new LambdaQueryWrapperPlus<>(ShopCollect.class)
                .eq(ShopCollect::getShopId, shopId)
                .eq(ShopCollect::getUserId, UserProvider.currentUserId()));
    }

}
